package com.iecas.handler;

import com.iecas.context.GlobalContext;
import com.iecas.context.ServerContext;
import com.iecas.entity.Bullet;
import com.iecas.entity.Tank;
import com.iecas.enums.BizEnum;
import com.iecas.enums.Direction;
import com.iecas.enums.ResponseEnum;
import com.iecas.launch.Client;
import com.iecas.launch.Server;
import com.iecas.request.FireReq;
import com.iecas.request.MoveReq;
import com.iecas.request.Root;
import com.iecas.response.BaseResponse;
import com.iecas.response.FireResp;
import com.iecas.response.MoveResp;

import java.nio.channels.SocketChannel;

public class FireReqHandler extends BaseHandler {

    @Override
    BaseResponse handle(Root baseRequest, Server server, Client client, SocketChannel socketChannel) {
        FireReq request = (FireReq) baseRequest;
        String token = request.getToken();

        Tank tank = GlobalContext.getServerContext().getTank(token);
        if (tank.canFire()) {
            Bullet bullet = tank.fire();
            ServerContext serverContext = GlobalContext.getServerContext();
            serverContext.getBulletList().add(bullet);
        }
        FireResp resp = new FireResp();
        resp.setStatus(ResponseEnum.SUCCESS.getCode());
        resp.setCode(BizEnum.FIRE_RESP.getCode());
        return resp;
    }
}
